//
//  Category_GoodsView.m
//  MindBox
//
//  Created by MS on 16-1-5.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Category_GoodsView.h"
#import "UIView+Frame.h"
#import "HeaderReusableView.h"
#import "UIImageView+WebCache.h"
#import "CategoryGoodsCell.h"
#define IMAGH 65
#define MARGIN 10

@interface Category_GoodsView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,weak) UIImageView * imageView;
@property (nonatomic,strong) UILabel * titleView;
@end

@implementation Category_GoodsView

-(void)setCategory_case:(Category_Case *)category_case
{
    _category_case = category_case;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:category_case.image_url]];
}


-(UILabel *)titleView
{
    if (_titleView==nil) {
        UILabel * lab = [[UILabel alloc] init];
        _titleView = lab;
    }
    return _titleView;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UICollectionViewFlowLayout * viewLayout = [[UICollectionViewFlowLayout alloc] init];
        viewLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        viewLayout.minimumLineSpacing = 10;
        viewLayout.minimumInteritemSpacing = 0;
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:viewLayout];
        self.collectionView = collectionView;
        self.collectionView.contentInset = UIEdgeInsetsMake(IMAGH+MARGIN, 0, 64, 0);
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.alwaysBounceVertical = YES;
        collectionView.backgroundColor = [UIColor whiteColor];
        [self addSubview:collectionView];
        
        UIImageView * imageView = [[UIImageView  alloc] init];
        [collectionView addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.imageView = imageView;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = YES;
        
        }
    return self;
}


-(void)tapClick
{
    if ([self.delegate respondsToSelector:@selector(category_GoodsViewDidClick:categoryGoods:categoryCase:index:)]) {
        [self.delegate category_GoodsViewDidClick:self categoryGoods:nil categoryCase:self.category_case index:-1];
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.category_goods.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    Category_Goods * goods = self.category_goods[section];
    
    return goods.son_category.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryGoodsCell * cell = [CategoryGoodsCell cellWithcollectionView:collectionView cellForItemAtIndexPath:indexPath];
    
    Category_Goods  * goods = self.category_goods[indexPath.section] ;

    cell.son_category = goods.son_category[indexPath.item];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.width, 30);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((self.width - 30)/3, 100);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeaderReusableView * headerView = [HeaderReusableView headerReusableViewWithCollectionView:collectionView viewForSupplementaryElementOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
     headerView.name =[self.category_goods[indexPath.section] name];
    
    return headerView;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Category_Goods  * goods = self.category_goods[indexPath.section] ;
    
    if ([self.delegate respondsToSelector:@selector(category_GoodsViewDidClick:categoryGoods:categoryCase:index:)]) {
        [self.delegate category_GoodsViewDidClick:self categoryGoods:goods.son_category[indexPath.item] categoryCase:nil index:indexPath.item];
    }
    
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
    self.imageView.frame = CGRectMake(10, -IMAGH, self.width-20, IMAGH);
   
}

@end
